<template>
  <div>
    <div class="title">
        <span>分账统计</span>
        <span :key="key"></span>
        <div>
            <el-button type="primary" :disabled="exxxx" @click="maniexcelBtn">
              <i class="el-icon-download"></i>导出
            </el-button>
            <el-button
            icon="el-icon-refresh"
            @click.native="updateClick()"
            ></el-button>
        </div>
    </div>
    <div class="container">
      <!-- 搜索板块 -->
      <div class="screeningBox">
        <el-form ref="searchObj" :model="searchObj">
              <div class="leftBox">
                <div class="leftitem">
                    <el-form-item style="width:65%" prop="merchantNo">
                        <el-input placeholder="商户号" v-model="searchObj.merchantNo"></el-input>
                    </el-form-item>
                </div>
                <div class="leftitem">
                    <el-form-item style="width:65%" prop="merchantName">
                        <el-input placeholder="商户名" v-model="searchObj.merchantName"></el-input>
                    </el-form-item>
                </div>
              </div>
              <div class="rightBox">
                  <div class="leftitem">
                    <el-form-item prop="activityName" style="width: 40%">
                        <el-date-picker
                        v-model="date"
                        style="width: 100%"
                        type="daterange"
                        @change="dateChange"
                        :picker-options="pickerOptions"
                        value-format="yyyy-MM-dd"
                        range-separator="至"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        :clearable="false"
                        :default-time="['00:00:00', '23:59:59']"
                        >
                        </el-date-picker>
                    </el-form-item>
                  </div>
                  <div class="leftitem">
                    <el-form-item>
                        <el-button icon="el-icon-search" type="primary" @click="soso" :disabled="isDisable">
                            搜索
                        </el-button>
                        <el-button icon="el-icon-refresh" type="primary" @click="reset">重置</el-button>
                    </el-form-item>
                  </div>
              </div>
          </el-form>
      </div>
      <div class="stitle">合计:</div>
      <div class="statistical">
        <div class="statisticalitem">
          <div>分账金额</div>
          <div>
            <span class="red">{{ totalCount.splitAmount > 0 ? totalCount.splitAmount / 100 : totalCount.splitAmount }}</span> 元
          </div>
        </div>
        <div class="statisticalitem">
          <div>手续费金额</div>
          <div>
            <span class="red">{{ totalCount.fee > 0 ? totalCount.fee / 100 : totalCount.fee }}</span> 元
          </div>
        </div>
        <div class="statisticalitem">
          <div>应结算金额</div>
          <div>
            <span class="red">{{ totalCount.balace > 0 ? totalCount.balace / 100 : totalCount.balace }}</span> 元
          </div>
        </div>
      </div>
    </div>
    <!-- 表格板块 -->
    <div class="tableBox">
        <el-table
            ref="multipleTable"
            :data="yztData"
            tooltip-effect="dark"
            style="width: 100%"
            @selection-change="handleSelectionChange"
            stripe
            :loading="loading"
            :header-cell-style="tableHeaderColor">
        <el-table-column type="selection" width="55"></el-table-column>
     
        <el-table-column
            prop="merchantNo"
            label="商户号"
            align="center"
        ></el-table-column>
        <el-table-column
            prop="merchantName"
            label="商户名"
            align="center"
        ></el-table-column>
        <el-table-column
          prop="splitAmount"
          label="分账金额"
          align="center">
          <template slot-scope="scope">
            <div>
              {{ scope.row.splitAmount / 100 }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="fee"
          label="手续费金额"
          align="center">
          <template slot-scope="scope">
            <div>
              {{ scope.row.fee / 100 }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="balace"
          label="应结算金额"
          align="center">
          <template slot-scope="scope">
            <div>
              {{ scope.row.balace / 100 }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
            fixed="right"
            align="center"
            label="操作"
            width="120"
        >
            <template slot-scope="scope">
                <el-button
                    type="text"
                    size="small"
                    @click="yztDetail(scope.row)">
                    详情
                </el-button>
            </template>
        </el-table-column>
    </el-table>
    <div class="pagination">
    <el-pagination
        background
        @current-change="handleCurrentChange"
        @size-change="sizechange"
        :page-sizes="[10,20,30,40,50]"
        :page-size='cur_size'
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
    ></el-pagination>
    </div>
    </div>
  </div>
</template>

<script>
import { merSplitCount, splitexcel, splitTotalCount } from "@/api/yztmanage/yztmanage";
import { getTimeFn } from '@/utils/getday.js'
export default {
  name: 'yztmanage',

  data() {
    return {
      pickerOptions: {
          // 设置不能超过未来时间
          // disabledDate: (time) => {
          //   // 设置可选择的日期为今天之后的一个月内
          //   const curDate = (new Date()).getTime()
          //   // 这里算出一个月的毫秒数,这里使用30的平均值,实际中应根据具体的每个月有多少天计算
          //   const day = 31 * 24 * 3600 * 1000
          //   const dateRegion = curDate - day
          //   return time.getTime() > Date.now() - 8.64e7 ||
          //   time.getTime() < dateRegion
          // }
          // 设置不能超过当天的23:59:59
          disabledDate(time) {
          let nowDate = new Date();
          let date = {
              year: nowDate.getFullYear(),
              month: nowDate.getMonth() + 1,
              date: nowDate.getDate(),
          };
          let timeDate =
              date.year + "-" + date.month + "-" + date.date + " 23:59:59";
          let times = new Date(timeDate).getTime();
          return time.getTime() > times;
          },
      },
      key: 0,
      loading: false,
      isDisable: false,
      exxxx: false,
      searchObj: {},
      date: [],
      yztData: [],
      totalCount: {},
      cur_page: 1,
      cur_size: 10,
      total: 0
    };
  },

  created() {
    const time = new Date().getTime() - (24 * 60 * 60 * 1000)
    let starttime = getTimeFn(time)
    const date1 = starttime;
    const date2 = starttime;
    this.date = [date1, date2];

    this.getManiCount()
    this.getTotalCount()
  },

  methods: {
    //修改thead 背景色
    tableHeaderColor({ row, column, rowIndex, columnIndex }) {
        if (rowIndex === 0) {
            return "background-color: #F0F0F0;font-weight: 500;font-size:14px;color:#000;";
        }
    },
    // 分页导航
    handleCurrentChange(val) {
        this.cur_page = val;
        this.$nextTick(() => {
            document.querySelectorAll(".content")[0].scrollTop = "0";
        });
    },
    // 分页数量
    sizechange(val) {
        this.cur_size = val;
        this.$nextTick(() => {
            document.querySelectorAll(".content")[0].scrollTop = "0";
        });
    },
    //多选框
    handleSelectionChange(val) {
        this.multipleSelection = val;
    },
    // 刷新
    updateClick() {
      this.getManiCount()
      this.getTotalCount()
    },

    // 搜索
    soso() {
      this.cur_page = 1
      this.getManiCount()
      this.getTotalCount()
    },

    // 重置
    reset() {
      const time = new Date().getTime() - (24 * 60 * 60 * 1000)
      let starttime = getTimeFn(time)
      const date1 = starttime;
      const date2 = starttime;
      this.date = [date1, date2];
      this.$refs.searchObj.resetFields()
    },

    // 时间筛选组件change事件
    dateChange(e) {
      const starttime = Date.parse(new Date(e[0]))
      const finishtime = Date.parse(new Date(e[1]))
      if(finishtime - starttime > 2592000000) {
        this.$message.error('筛选时间不能超过30天')
        const time = new Date().getTime() - (24 * 60 * 60 * 1000)
        let starttime = getTimeFn(time)
        const date1 = starttime;
        const date2 = starttime;
        this.date = [date1, date2];
      }
    },

    // 详情
    yztDetail(e) {
      console.log(e);
      let info = {
        id: e.merchantId,
        startDate: this.date[0],
        endDate: this.date[1],
        merchantName: this.searchObj.merchantName,
        merchantNo: this.searchObj.merchantNo,
        merchantId: this.searchObj.merchantId,
      }
      this.$router.push({
        path: '/splitcountdetail',
        query: {
          info: JSON.stringify(info)
        }
      })
    },

    // 查询货单统计（按日期）
    async getManiCount() {
      this.loading = true;
      let res = await merSplitCount({
        page: this.cur_page,
        size: this.cur_size,
        startDate: this.date[0],
        endDate: this.date[1],
        merchantName: this.searchObj.merchantName,
        merchantNo: this.searchObj.merchantNo,
        merchantId: this.searchObj.merchantId,
      })
      if(res.code == 405) {
        localStorage.removeItem('token')
        this.$router.push('/login');
        this.loading = false
      } else {
        if(res.code == 200) {
          this.yztData = res.data.data
          this.total = res.data.pageInfo.recordCount
          this.cur_page = res.data.pageInfo.pageCurrent
          this.cur_size = res.data.pageInfo.pageSize
          this.loading = false
        } else {
          this.$message.error(res.message)
          this.loading = false
        }
      }
    },

    // 总统计信息
    async getTotalCount() {
      let res = await splitTotalCount({
        startDate: this.date[0],
        endDate: this.date[1],
        merchantName: this.searchObj.merchantName || '',
        merchantNo: this.searchObj.merchantNo || '',
        merchantId: this.searchObj.merchantId || '',
      })
      if(res.data.code == 405) {
        localStorage.removeItem('token')
        this.$router.push('/login');
        this.loading = false
      } else {
        if(res.data.code == 200) {
          console.log(res);
          this.totalCount = {...res.data.data.data}
          this.loading = false
        } else {
          this.$message.error(res.data.message)
          this.loading = false
        }
      }
    },

    // 导出
    async maniexcelBtn() {
      let res = await splitexcel({
        page: this.cur_page,
        size: this.cur_size,
        startDate: this.date[0],
        endDate: this.date[1],
        merchantName: this.searchObj.merchantName,
        merchantNo: this.searchObj.merchantNo,
        merchantId: this.searchObj.merchantId,
      })
      this.exxxx = true;
      let i = 0;
      let time = setInterval(() => {
        if (i == 60) {
          loading.close();
          this.$message.error("请求超时");
          clearInterval(time);
        } else {
          i++;
        }
      }, 1000);
      const loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      if(res.data.code == 405) {
        clearInterval(time);
        localStorage.removeItem('token')
        this.$router.push('/login');
        loading.close();
        this.exxxx = false;
      } else {
        if(res.data.code == 200) {
          clearInterval(time);
          let elink = document.createElement("a");
          elink.download = "一账通对账单.xls";
          elink.href = res.data.data;
          elink.click();
          loading.close();
          this.exxxx = false;
        } else {
          clearInterval(time);
          this.$message.error(res.data.message);
          loading.close();
          this.exxxx = false;
        }
      }
    }
  },
};
</script>

<style lang="less" scoped>
.title {
  background: #fff;
  padding: 10px 10px;
  font-size: 18px;
  font-weight: 500;
  display: flex;
  justify-content: space-between;
}
.screeningBox {
  width: 100%;
  display: flex;
}
.leftBox,
.rightBox {
  width: 40%;
  padding: 0 15px;
  box-sizing: border-box;
}
.rightBox {
    width: 70%;
}
.leftBox > .leftitem,
.rightBox > .leftitem {
  display: flex;
}

.screeningBox .el-form{
    display: flex;
    width: 100%;
}
.ipt {
    width: 70%;
    margin-right: 10px;
    margin-bottom: 15px;
}
.stitle {
  padding: 5px 0;
  font-size: 18px;
}
.statistical {
  width: 100%;
  display: flex;
  justify-content: space-around;
  align-items: center;
  background: #00152a;
  color: #fff;
  padding: 12px;
  box-sizing: border-box;
}
.statistical > .statisticalitem {
  text-align: center;
  line-height: 200%;
}
.red {
  color: #f9607d;
}
</style>